Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@contrast/require-hook
Advanced tools
Intercept calls to require
in order to modify or replace exports.
RequireHook
const RequireHook = require('./lib');
const requireHook = new RequireHook();
The RequireHook
constructor accepts a pino
logger as an argument.
.resolve(descriptor, ...handlers)
Options:
descriptor
: This can be a string or an object describing the module you want
to intercept. If a string is used, or if the version field of the descriptor
isn't set, all versions of the described module will be matched. Descriptors
can have a name
, version
, and file
property.
handlers
: The remaning arguments are the handlers which will be invoked when
the described module is require
'd. Each handler is passed the exported
module and metadata including the module's root directory and its name and
version as seen in its package.json
file. If a handler returns a truthy
value, then that value will replace the return value of require
.
Note: Registered handlers run once per unique instance of an export matching a descriptor.
.install()
This will monkey-patch Module.prototype.require
so that exports can be
intercepted. The monkey-patching will only happen once regardless of how many
times this is invoked.
.uninstall()
This will reset Module.prototype.require
to its value before being
monkey-patched by the instance.
Use case: For express
versions greater than or equal to 4, intercept the
export of the package's lib/view.js
file (relative to the package's base
directory) and apply a tag to the exported function.
const RequireHook = require('./lib');
const requireHook = new RequireHook();
requireHook.resolve(
{
name: 'express',
version: '>=4',
file: 'lib/view.js',
},
(xport, metadata) => {
// Read from the package.json:
// - metadata.name
// - metadata.version
// Absolute path to file:
// - metadata.packageDir
// xport === function View() { /*...*/ }
xport['I was intercepted'] = true;
},
);
Use case: Intercept all versions of body-parser
and replace the exported
functions.
const RequireHook = require('./lib');
const requireHook = new RequireHook();
requireHook.resolve({ name: 'body-parser' }, (xport, metadata) => {
// Read from the package.json:
// - metadata.name
// - metadata.version
// Absolute path to file:
// - metadata.packageDir
// xport === function bodyParser() { /*...*/ }
return function bodyParserReplacement() {
/*...*/
};
});
FAQs
Post hooks for Module.prototype.require
The npm package @contrast/require-hook receives a total of 5,442 weekly downloads. As such, @contrast/require-hook popularity was classified as popular.
We found that @contrast/require-hook demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.